//
//  WGPOSTHook.m
//  WeChatSp_dylib
//
//  Created by 丝瓜&冬瓜 on 15/11/23.
//  Copyright (c) 2015年 ABC. All rights reserved.
//

#import "SoHuHook.h"
#import <objc/runtime.h>


@interface NSObject (Hook)

@end

@implementation NSObject (Hook)

#pragma mark - VideoBaseInfo
- (bool) Sohu_isDownloadable{
    return TRUE;
}
#pragma mark - BaseVideoItem
//- (bool) Sohu_isDownloadable{
//    return TRUE;
//}
#pragma mark - LaunchAdvertItem
- (bool)Sohu_existsLaunchAdvertImage{
    return FALSE;
}
#pragma mark - ConfigurationCenter
- (bool)Sohu_shouldLoadAdvertForLocalVideo{
    return FALSE;
}
- (id )Sohu_advertUrlBase{
    return nil;
}


#pragma mark - swizzling
+ (IMP)swizzleSelector:(SEL)origSelector
               withIMP:(SEL)newSelector {
    Class class = [self class];
    Method origMethod = class_getInstanceMethod(class,
                                                origSelector);
    IMP origIMP = method_getImplementation(origMethod);
    if (!origIMP) {
        NSLog(@"------------%@ has no org method:%@",NSStringFromClass(self),NSStringFromSelector(origSelector));
    }
    
    if(!class_addMethod(self, origSelector, class_getMethodImplementation(class, newSelector),
                        method_getTypeEncoding(origMethod)))
    {
        NSLog(@"添加方法失败");
        method_exchangeImplementations(
                                       class_getInstanceMethod(class, origSelector),
                                       class_getInstanceMethod(class, newSelector
                                                               ));
        //        method_setImplementation(origMethod, newIMP);
    }else{
        NSLog(@"成功添加，交换IMP");
    }
    
    return origIMP;
}
@end

#pragma mark -
@implementation SoHuHook
+ (void)load{
    [super load];
    
    [self hookPOST];
}
+ (void)hookPOST{
    NSLog(@"-----------------sohu hook----------------");
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        Class aClass;
        
        aClass = NSClassFromString(@"VideoBaseInfo");
        [aClass swizzleSelector:@selector(isDownloadable)
                        withIMP:@selector(Sohu_isDownloadable)];
                if (!aClass) NSLog(@"---------%@ not exist",NSStringFromClass(aClass));
        
        
        aClass = NSClassFromString(@"BaseVideoItem");
        [aClass swizzleSelector:@selector(isDownloadable)
                        withIMP:@selector(Sohu_isDownloadable)];
        if (!aClass) NSLog(@"---------%@ not exist",NSStringFromClass(aClass));

        aClass = NSClassFromString(@"LaunchAdvertItem");
        [aClass swizzleSelector:@selector(existsLaunchAdvertImage)
                        withIMP:@selector(Sohu_existsLaunchAdvertImage)];
        if (!aClass) NSLog(@"---------%@ not exist",NSStringFromClass(aClass));

        aClass = NSClassFromString(@"ConfigurationCenter");
        [aClass swizzleSelector:@selector(shouldLoadAdvertForLocalVideo)
                        withIMP:@selector(Sohu_shouldLoadAdvertForLocalVideo)];
        [aClass swizzleSelector:@selector(advertUrlBase)
                        withIMP:@selector(Sohu_advertUrlBase)];
        if (!aClass) NSLog(@"---------%@ not exist",NSStringFromClass(aClass));

    });

}
@end





